<?php

// Model:'ChiTietCoSoThanhTra' - Database Table: 'chi_tiet_cs_tt'

Class ChiTietCoSoThanhTra extends Eloquent {

    //Cac bang tham gia trong model
    private $tables = array("cs_tt", "chi_tiet_cs_tt");
    //Rang buoc du lieu
    private $rules = array(
        "NGANH_NGHE_KD" => "required|min:1|max:50",
        "THOI_DIEM_KT_TU" => "required",
        "THOI_DIEM_KT_DEN" => "required",
        "HT_HACH_TOAN" => "required|min:1|max:50",
        "HT_NOP_THUE" => "required|min:1|max:50",
        "NOI_DK_KE_KHAI" => "required|min:1|max:60"
    );

    //Ham kiem tra du lieu
    public function validation($data) {
        return Validator::make($data, $this->rules);
    }

    //Lay ds du lieu
    public function GetList() {
        
    }

    //Lay du lieu xac dinh
    public function GetOne($id) {
        return DB::table($this->tables[0])
                        ->join($this->tables[1], function($join) {
                            $join->on($this->tables[0] . '.ID', '=', $this->tables[1] . '.ID_CS_TT');
                        })
                        ->select($this->tables[0] . ".ID", $this->tables[0] . ".MST", $this->tables[0] . ".TEN_CS", $this->tables[0] . ".CHU_CS", $this->tables[0] . ".GIAY_CN_SO", $this->tables[0] . ".NOI_CAP", $this->tables[0] . ".NGAY_CAP", $this->tables[0] . ".DIA_CHI", $this->tables[1] . ".NGANH_NGHE_KD", $this->tables[1] . ".THOI_DIEM_KT_TU", $this->tables[1] . ".THOI_DIEM_KT_DEN", $this->tables[1] . ".HT_HACH_TOAN", $this->tables[1] . ".HT_NOP_THUE", $this->tables[1] . ".NOI_DK_KE_KHAI", DB::raw($this->tables[1] . ".ID as ID_CT_CS_TT"))
                        ->where($this->tables[1] . ".ID", $id)
                        ->first();
    }

    public function GetOneWithMST($mst) {

        return DB::table($this->tables[0])
                        ->join($this->tables[1], function($join) {
                            $join->on($this->tables[0] . '.ID', '=', $this->tables[1] . '.ID_CS_TT');
                        })
                        ->select($this->tables[0] . ".ID",DB::raw($this->tables[1] . ".ID as ID_CS_TT"), $this->tables[0] . ".TEN_CS")
                        ->where($this->tables[1] . ".MST", "LIKE", $mst)
                        ->first();
    }

    //Luu du lieu moi
    public function SaveIn($data) {
        return DB::table($this->tables[1])
                        ->insert($data);
    }

    //Luu du lieu sua doi
    public function StoreIn($data, $id) {
        try {
            DB::table($this->tables[1])
                    ->where("ID", $id)
                    ->update($data);
            return true;
        } catch (Exception $ex) {
            return false;
        }
    }

    //Xoa du lieu xac dinh
    public function Remove($id) {
        try {
            return DB::table($this->tables[1])
                            ->delete($id);
        } catch (Exception $ex) {
            return false;
        }
    }

}
